﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OneSoft.DataAccess;
using BySoft.Server.Common.WebCommon;
using System.Data;
using BySoft.Server.Common.Schema;
using BySoft.Server.Business;
using BySoft.Server.Common.Enum;
using System.Collections;
using BySoft.Server.Common.Entity;

namespace BySoft.Server.Business
{
    public class SelectionCatche
    {

        #region news
        //public static DataTable GetNews(string strCount)
        //{
        //    if (WebCaching.ALLNEWSCaching == null)
        //        SysNews.RefreshWebCaching(strCount);
        //    DataTable dt = new DataTable();
        //    DataSet ds = (DataSet)WebCaching.ALLNEWSCaching;
        //    if (ds != null && ds.Tables.Count != 0 && ds.Tables[0].Rows.Count > 0)
        //    {
        //        DataView dv = new DataView(ds.Tables[0]);
        //        dt = dv.ToTable();
        //    }
        //    return dt;
        //}
        #endregion


        #region notices
        //public static DataTable GetNotices(string strCount)
        //{
        //    if (WebCaching.ALLNOTICESCaching == null)
        //        SysNotice.RefreshWebCaching(strCount);
        //    DataTable dt = new DataTable();
        //    DataSet ds = (DataSet)WebCaching.ALLNOTICESCaching;
        //    if (ds != null && ds.Tables.Count != 0 && ds.Tables[0].Rows.Count > 0)
        //    {
        //        DataView dv = new DataView(ds.Tables[0]);
        //        dt = dv.ToTable();
        //    }
        //    return dt;
        //}
        #endregion

        #region 参数、参数类别数据
        /// <summary>
        ///   根据参数类别代号获取参数，数据来源于缓存
        /// </summary>
        /// <param name="strTypeID">参数代号</param>
        /// <returns></returns>
        public static DataTable GetParametersByType(string strTypeID)
        {
            if (WebCaching.ALLPARAMETERSCaching == null)
                SysParameter.RefreshWebCaching();
            DataTable dt = new DataTable();
            DataSet ds = (DataSet)WebCaching.ALLPARAMETERSCaching;
            if (ds != null && ds.Tables.Count != 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataView dv = new DataView(ds.Tables[0]);
                dv.RowFilter = SysParameterTable.C_PARAMETERTYPEID + "='" + strTypeID + "' and " + SysParameterTable.C_STATUS + "='" + ((int)BaseEnum.CommonSatus.Started).ToString() + "'";
                dv.Sort = SysParameterTable.C_ORDERNUMBER;
                dt = dv.ToTable();
            }
            return dt;
        }
        #endregion

        ///// <summary>
        /////   根据条件查询参数
        ///// </summary>
        ///// <param name="strTypeID">条件</param>
        ///// <returns></returns>
        //public static DataTable GetParametersByCondition(string condition)
        //{
        //    if (WebCaching.ALLPARAMETERSCaching == null)
        //        SysParameter.RefreshWebCaching();
        //    DataTable dt = new DataTable();
        //    DataSet ds = (DataSet)WebCaching.ALLPARAMETERSCaching;
        //    if (ds != null && ds.Tables.Count != 0 && ds.Tables[0].Rows.Count > 0)
        //    {
        //        DataView dv = new DataView(ds.Tables[0]);
        //        dv.RowFilter = condition;
        //        dv.Sort = SysParameterTable.C_ORDERNUMBER;
        //        dt = dv.ToTable();
        //    }
        //    return dt;
        //}


        ///// <summary>
        /////   根据参数类别代号获取参数，返回SortedList,SortedList的key为参数代码，value为参数名称
        ///// </summary>
        ///// <param name="strTypeID">参数代号</param>
        ///// <returns>SortedList</returns>
        //public static SortedList GetSLParametersByType(string strTypeID)
        //{
        //    DataTable dt = GetParametersByType(strTypeID);
        //    SortedList sl = new SortedList();
        //    if (dt != null && dt.Rows.Count != 0)
        //    {
        //        foreach (DataRow row in dt.Rows)
        //        {
        //            sl.Add(row[SysParameterTable.C_PARAMETERCODE].ToString(), row[SysParameterTable.C_PARAMETERNAME]);
        //        }
        //    }
        //    return sl;
        //}

        /// <summary>
        ///  根据id获取参数类别,数据来源于缓存
        /// </summary>
        /// <param name="strID">参数代号</param>
        /// <returns>SysParametertypeEntity</returns>
        public static SysParametertypeEntity GetParameterTypeByID(string strID)
        {
            SysParametertypeEntity entity = null;
            DataTable dt = GetParameterTypeAll();
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow[] rows = dt.Select(SysParametertypeTable.C_PARAMETERTYPEID + "='" + strID + "'");
                if (rows.Length != 0)
                {
                    entity = new SysParametertypeEntity();
                    DataRow objRow = rows[0];
                    entity.ConvertEntity(objRow);
                    //entity.CreatorName = rows[0]["CreatorName"].ToString();
                    //entity.ModifyUserName = rows[0]["ModifyUserName"].ToString();
                }
            }
            return entity;
        }

        #region GetParameterTypeAll
        /// <summary>
        ///  获取所有参数类别数据，数据来源于缓存
        /// </summary>
        /// <returns></returns>
        public static DataTable GetParameterTypeAll()
        {
            if (WebCaching.ALLPARAMETERTYPESCaching == null)
                SysParametertype.RefreshWebCaching();
            DataTable dt = null;
            DataSet ds = (DataSet)WebCaching.ALLPARAMETERTYPESCaching;
            if (ds != null && ds.Tables.Count != 0)
            {
                dt = ds.Tables[0];
            }
            return dt;
        }
        #endregion

        /// <summary>
        ///  查询，参数值为空查询所有
        /// </summary>
        /// <param name="strParameterTypeName"></param>
        /// <returns></returns>
        public static DataTable QueryParamerterType(string strTypeID, string strParameterTypeName)
        {
            DataTable dt = GetParameterTypeAll();
            DataView dv = new DataView(dt);
            string strFilter = SysParametertypeTable.C_STATUS + "<>'" + ((int)BaseEnum.CommonSatus.Delete).ToString() + "'";
            if (strParameterTypeName != "")
            {
                strFilter += " and " + SysParametertypeTable.C_PARAMETERTYPENAME + " like '%" + strParameterTypeName.Replace("'", "''").Replace("%", "[%]") + "%'";
            }
            if (!string.IsNullOrEmpty(strTypeID))
            {
                strFilter += " and " + SysParametertypeTable.C_PARAMETERTYPEID + " like '" + strTypeID.Replace("'", "''").Replace("%", "[%]") + "%'";
            }
            dv.RowFilter = strFilter;
            return dv.ToTable();
        }

        #region 参数
        /// <summary>
        ///  根据参数ID查询,来源于缓存
        /// </summary>
        /// <param name="strID">参数ID</param>
        /// <returns></returns>
        public static SysParameterEntity GetParamerterByID(int id)
        {
            DataTable dt = GetParameterAll();
            SysParameterEntity entity = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                entity = new SysParameterEntity();
                DataRow[] rows = dt.Select(SysParameterTable.C_PARAMETERID + "=" + id.ToString());
                if (rows.Length > 0)
                {
                    entity.ConvertEntity(rows[0]);

                }
            }
            return entity;
        }

        /// <summary>
        ///   根据参数类别、参数代号获取参数名称
        /// </summary>
        /// <param name="strTypeID">类别ID</param>
        /// <param name="strParameterCode">参数代码</param>
        /// <returns></returns>
        public static String GetParameterName(string strTypeID, string strParameterCode)
        {
            String strResult = null;
            DataTable dt = GetParameterAll();
            DataRow[] rows = dt.Select(SysParameterTable.C_PARAMETERTYPEID + "='" + strTypeID + "' and " +
                              SysParameterTable.C_PARAMETERCODE + "='" + strParameterCode.Replace("'", "''") + "'");
            if (rows.Length > 0)
            {
                strResult = rows[0][SysParameterTable.C_PARAMETERNAME].ToString();
            }
            return strResult;

        }

        /// <summary>
        ///   根据参数类别、参数代号获取参数名称
        /// </summary>
        /// <param name="strTypeID">类别ID</param>
        /// <param name="strParameterCode">参数代码</param>
        /// <returns></returns>
        public static String GetParameterValue(string strTypeID, string strParameterCode)
        {
            String strResult = null;
            DataTable dt = GetParameterAll();
            DataRow[] rows = dt.Select(SysParameterTable.C_PARAMETERTYPEID + "='" + strTypeID + "' and " +
                              SysParameterTable.C_PARAMETERCODE + "='" + strParameterCode.Replace("'", "''") + "'");
            if (rows.Length > 0)
            {
                strResult = rows[0][SysParameterTable.C_PARATMETERVALUE].ToString();
            }
            return strResult;

        }

        /// <summary>
        ///   根据参数类别、参数代号获取参数实体
        /// </summary>
        /// <param name="strTypeID">类别ID</param>
        /// <param name="strParameterCode">参数代码</param>
        /// <returns>参数实体</returns>
        public static SysParameterEntity GetParameter(string strTypeID, string strParameterCode)
        {
            SysParameterEntity entity = null;
            DataTable dt = GetParameterAll();
            DataRow[] rows = dt.Select(SysParameterTable.C_PARAMETERTYPEID + "='" + strTypeID + "' and " +
                              SysParameterTable.C_PARAMETERCODE + "='" + strParameterCode.Replace("'", "''") + "'");
            if (rows.Length > 0)
            {
                entity = new SysParameterEntity();
                entity.ConvertEntity(rows[0]);
            }
            return entity;

        }

        /// <summary>
        ///   获取所有参数,数据来源于缓存
        /// </summary>
        /// <returns></returns>
        public static DataTable GetParameterAll()
        {
            if (WebCaching.ALLPARAMETERSCaching == null)
                SysParameter.RefreshWebCaching();
            DataSet ds = (DataSet)WebCaching.ALLPARAMETERSCaching;
            if (ds != null && ds.Tables.Count != 0)
            {
                return ds.Tables[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        ///   查询，参数为空查询所有
        /// </summary>
        /// <param name="strParaTypeName">类别名称</param>
        /// <returns></returns>
        public static DataTable QueryParameter(string strParaTypeName, string strCodeId, string strParameterName, string strIsValid)
        {
            DataTable dt = GetParameterAll();
            DataView dv = new DataView(dt);
            string strFilter = SysParameterTable.C_STATUS + "<>'" + ((int)BaseEnum.CommonSatus.Delete).ToString() + "'";
            if (strParaTypeName != "")
            {
                strFilter += " and " + SysParametertypeTable.C_PARAMETERTYPENAME + " like '%" + strParaTypeName.Replace("'", "''").Replace("%", "[%]") + "%'";
            }
            if (!string.IsNullOrEmpty(strCodeId))
            {
                strFilter += " and " + SysParameterTable.C_PARAMETERCODE + " like '" + strCodeId.Replace("'", "''").Replace("%", "[%]") + "%'";
            }
            if (!string.IsNullOrEmpty(strParameterName))
            {
                strFilter += " and " + SysParameterTable.C_PARAMETERNAME + " like '%" + strParameterName.Replace("'", "''").Replace("%", "[%]") + "%'";
            }
            if (!string.IsNullOrEmpty(strIsValid))
            {
                strFilter += " and " + SysParameterTable.C_STATUS + "='" + strIsValid.Replace("'", "''") + "'";
            }
            dv.RowFilter = strFilter;
            return dv.ToTable();
        }
        #endregion


        #region 模块
        public static SysModuleEntity GetModuleByUrl(string url)
        {
            SysModuleEntity enModule = new SysModuleEntity();
            if (WebCaching.ALLMODULESCaching == null)
            {
                SysModule.RefreshWebCaching();
            }
            DataTable dt = ((DataSet)WebCaching.ALLMODULESCaching).Tables[0];
            DataView dv = dt.DefaultView;
            dv.RowFilter = "pagetype<>'T'";
            string moduleUrl = string.Empty;
            string pageType = string.Empty;
            string urlTemp = url.ToLower();
            for (int i = 0; i < dv.Count; i++)
            {
                moduleUrl = dv[i][SysModuleTable.C_FUNCTIONURL].ToString().Trim().ToLower();
                if (string.IsNullOrEmpty(moduleUrl))
                {
                    continue;
                }
                pageType = dv[i][SysModuleTable.C_PAGETYPE].ToString().Trim();
                if (urlTemp.Contains(moduleUrl))
                {

                    if (pageType != "F")
                    {
                        enModule.ModuleID = dv[i][SysModuleTable.C_PARENTMODULEID].ToString();
                    }
                    else
                    {
                        enModule.ModuleID = dv[i][SysModuleTable.C_MODULEID].ToString();
                    }
                    //enModule.Moduleid = dv[i][SysModuleTable.C_MODULEID].ToString();
                    //enModule.Modulename = dv[i][SysModuleTable.C_MODULENAME].ToString();
                    break;
                }
            }
            dv.RowFilter = SysModuleTable.C_MODULEID + "='" + enModule.ModuleID + "'";
            if (dv.Count > 0)
            {
                enModule.ModuleName = dv[0][SysModuleTable.C_MODULENAME].ToString();
            }
            dv.RowFilter = "";
            return enModule;
        }

        public static DataTable GetSecondModuleList()
        {
            if (WebCaching.ALLMODULESCaching == null)
            {
                SysModule.RefreshWebCaching();
            }
            DataTable dt = ((DataSet)WebCaching.ALLMODULESCaching).Tables[0];
            DataView dv = dt.DefaultView;
            dv.RowFilter = "pagetype='F'";
            return dv.ToTable();
        }
        #endregion

        #region 前台菜单
        /// <summary>
        ///   根据父模块代号获取子菜单，数据来源于缓存
        /// </summary>
        /// <param name="strTypeID">父模块代号</param>
        /// <returns></returns>
        public static DataTable SelectTopMenuCaching(string sParentModuleID)
        {
            if (WebCaching.ALLTOPMENUCaching == null)
            {
                SysModule.RefreshTopMenuCaching(sParentModuleID);
            }
            DataTable dt = (DataTable)WebCaching.ALLTOPMENUCaching;
            return dt;
        }
        #endregion

        #region 科室专栏菜单
        /// <summary>
        ///   根据父模块代号获取子菜单，数据来源于缓存
        /// </summary>
        /// <param name="strTypeID">父模块代号</param>
        /// <returns></returns>
        public static DataTable SelectOfficeMenuCaching(string sParentModuleID)
        {
            if (WebCaching.ALLOFFICEMENUCaching == null)
            {
                SysModule.RefreshOfficeMenuCaching(sParentModuleID);
            }
            DataTable dt = (DataTable)WebCaching.ALLOFFICEMENUCaching;
            return dt;
        }
        #endregion
    }
}
